#!/usr/bin/env node

import { DatabaseManager } from './database-manager.js';
import { DatabaseMcpServer, DatabaseType } from './database-mcp-server.js';

// 添加进程错误处理
process.on('uncaughtException', (error) => {
  console.error('Uncaught Exception:', error);
});

process.on('unhandledRejection', (reason, promise) => {
  console.error('Unhandled Rejection at:', promise, 'reason:', reason);
});

process.on('SIGINT', () => {
  console.log('Received SIGINT. Graceful shutdown...');
  process.exit(0);
});

process.on('SIGTERM', () => {
  console.log('Received SIGTERM. Graceful shutdown...');
  process.exit(0);
});

// 启动统一的数据库服务器
export async function startDatabaseServer(): Promise<void> {
  try {
    console.log('Starting unified database server...');
    
    // 创建并启动统一的服务器
    const server = new DatabaseMcpServer();
    await server.start();
  } catch (error: any) {
    console.error('Error starting database server:', error.message);
    process.exit(1);
  }
}

// 导出所有模块
export {
  // 数据库管理器
  DatabaseManager,
  
  // 数据库服务器
  DatabaseMcpServer,
  DatabaseType
};

startDatabaseServer().catch(error => {
  console.error('Failed to start server:', error);
  process.exit(1);
});